package easy;

import util.PrintUtils;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class Solution_500 {

    public static Set<Character> firstSet = Arrays.stream(new Character[]{'q', 'Q', 'w', 'W', 'e', 'E', 'r', 'R', 't', 'T', 'y', 'Y', 'u', 'U', 'i', 'I', 'o', 'O', 'p', 'P'}).collect(Collectors.toSet());
    public static Set<Character> secondSet = Arrays.stream(new Character[]{'a', 'A', 's', 'S', 'd', 'D', 'f', 'F', 'g', 'G', 'h', 'H', 'j', 'J', 'k', 'K', 'l', 'L'}).collect(Collectors.toSet());
    public static Set<Character> thirdSet = Arrays.stream(new Character[]{'z', 'Z', 'x', 'X', 'c', 'C', 'v', 'V', 'b', 'B', 'n', 'N', 'm', 'M'}).collect(Collectors.toSet());

    public String[] findWords(String[] words) {
        int count = 0;
        String[] res = new String[words.length];
        for (String word : words) {
            Set<Character> set = null;
            boolean flag = true;
            char[] chars = word.toCharArray();
            for (char c : chars) {
                if (firstSet.contains(c)) {
                    if (set != null && firstSet != set) {
                        flag = false;
                        break;
                    }
                    set = firstSet;
                } else if (secondSet.contains(c)) {
                    if (set != null && secondSet != set) {
                        flag = false;
                        break;
                    }
                    set = secondSet;
                } else if (thirdSet.contains(c)) {
                    if (set != null && thirdSet != set) {
                        flag = false;
                        break;
                    }
                    set = thirdSet;
                }
            }
            if (flag) {
                res[count++] = word;
            }
        }
        String[] strings = new String[count];
        System.arraycopy(res, 0, strings, 0, count);
        return strings;
    }

    public static void main(String[] args) {
        Solution_500 model = new Solution_500();
//        String[] array = model.findWords(new String[]{"Hello","Alaska","Dad","Peace"});
        String[] array = model.findWords(new String[]{"omk"});
        PrintUtils.array(array);
    }
}
